package org.fastsyncer.manager.monitor;

import java.util.HashMap;
import java.util.Map;

import org.fastsyncer.common.entity.Mapping;
import org.fastsyncer.common.entity.MappingTask;
import org.fastsyncer.connector.framework.ConnectorFactory;
import org.fastsyncer.manager.driver.impl.AbstractDriverEvent;
import org.fastsyncer.manager.framework.DataFactory;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class MonitorFactory extends AbstractDriverEvent {

    private static final Logger logger = LoggerFactory.getLogger(MonitorFactory.class);
    
    private static volatile MonitorFactory instance = null;

    private MonitorFactory(){
    }
    
    public static MonitorFactory getInstance() {
        if (null == instance) {
            synchronized (MonitorFactory.class) {
                if (null == instance) {
                    instance = new MonitorFactory();
                }
            }
        }
        return instance;
    }
	
	// 驱动可用性
    private static final Map<String, Map<String, Object>> DRIVERS = new HashMap<String, Map<String, Object>>();
    
    private DataFactory data = DataFactory.getInstance();
    
    public void execute() throws JobExecutionException {
        // 启动驱动可用性监听服务,遍历检查所有驱动
        Map<String, Object> mappings = data.getMappings();
        if(null != mappings && !mappings.isEmpty()){
            try {
                // 遍历现有驱动列表
                MappingTask task;
                String id;
                Map<String, Object> t;
                for (Map.Entry<String, Object> object : mappings.entrySet()) {
                    task = (MappingTask) object.getValue();
                    id = task.getId();
                    // 排除禁用驱动
                    if(!task.isEnable()){
                        DRIVERS.remove(task.getId());
                        continue;
                    }
                    // 驱动状态信息
                    t = new HashMap<String, Object>();
                    // 检查数据源状态
                    t.put("source", this.test(task.getSourceMapping()));
                    // 检查目标源状态
                    t.put("target", this.test(task.getTargetMapping()));
                    
                    // 放入缓存
                    DRIVERS.put(id, t);
                }
            } catch (Exception e) {
                logger.error(e.getClass() + " >> " + e.getLocalizedMessage());
            }
        }
    }
    
	/**
	 * 测试驱动连接器配置是否可用
	 * @Title: test 
	 * @Description: 测试驱动连接器配置是否可用
	 * @param mapping 连接器配置
	 * @return: boolean 返回true/false
	 */
	private boolean test(Mapping mapping) {
	    String connector = mapping.getConnector();
	    Object config = mapping.getConfig();
		return ConnectorFactory.getInstance().isAlive(connector, config);
	}

    @Override
    public void delete(String id) {
        // 移除删除的驱动
        DRIVERS.remove(id);
    }

    /**
     * 获取指定驱动状态
     * @param id
     * @return
     */
    public Map<String, Object> getDriver(String id) {
        return DRIVERS.get(id);
    }

}
